package com.binarytree;

import java.util.ArrayList;
import java.util.List;

public class Solution25 {
    List<Integer> resList;
    int count;
    int max;
    TreeNode pre;
    public int[] findMode(TreeNode root) {
        resList = new ArrayList<>();
        count = 0;
        max = 0;
        get(root);
        int[] res = new int[resList.size()];
        for (int i = 0; i < res.length; i++) {
            res[i] = resList.get(i);
        }
        return res;
    }

    public void get(TreeNode root) {
        if (root == null) return;

        get(root.left);

        if (pre == null || root.val != pre.val) {
            count = 1;
        } else {
            count++;
        }
        if (count > max) {
            resList.clear();
            resList.add(root.val);
            max = count;
        } else if (count == max) resList.add(root.val);

        pre = root;
        get(root.right);
    }
}
